import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpEchoServer {
    //模拟实现一个UDP回显服务器；
    private DatagramSocket socket=null;
    public UdpEchoServer(int port) throws SocketException {
        socket=new DatagramSocket(port);
    }
    //进行启动服务器；
    public void start() throws IOException {
        System.out.println("启动服务器");
        while (true) {
            DatagramPacket requirePacket = new DatagramPacket(new byte[4096], 4096);
            socket.receive(requirePacket);
            String request=new String(requirePacket.getData(),0, requirePacket.getLength());
            String response=process(request);
            DatagramPacket responsePcket=new DatagramPacket(response.getBytes(),response.getBytes().length,
                    requirePacket.getSocketAddress());
            socket.send(responsePcket);
            System.out.printf("[%s:%d] request:%s,response:%s\n",
                    responsePcket.getAddress(),responsePcket.getPort());

        }
    }
    public String process(String request) {
        return request;
    }
    public static void main(String[] args) throws IOException {
        UdpEchoServer server=new UdpEchoServer(9090);
        server.start();
    }

}
